<?php
    /**
     * Created by PhpStorm.
     * User: Administrator
     * Date: 2017/12/26
     * Time: 15:11
     */

    class Category_model extends CI_Model
    {
        public $limit_rows = 15;

        public $table_name = 'pcs_category';

        /**
         * 根据分类名获取cateId
         *
         * @param $cate_name
         *
         * @return mixed
         * author: walker
         * Date: 2019/11/6
         * Time: 15:30
         * Note:
         */
        public function cate_id($cate_name)
        {
            $this->db->from('category');
            $this->db->where('cate_name', $cate_name);
            $query      = $this->db->get();
            $order_info = $query->row_array();
            return $order_info['cate_id'];
        }

        /**
         * @param $cate_name
         *
         * @return mixed
         * author: walker
         * Date: 2019/11/6
         * Time: 15:30
         * Note:
         */
        public function ls_cate_id($cate_name)
        {
            $this->db->from('category');
            $this->db->where('cate_name', $cate_name);
            $query      = $this->db->get();
            $order_info = $query->row_array();
            return $order_info['cate_id'];
        }

        public function cate_option()
        {

            $query = $this->db->from('category')->where(['is_delete' => 0, 'pid <>' => 0, 'is_show' => 0])->get();

            $cate = array();
            foreach ($query->result_array() as $row) {
                $cate[$row['cate_id']] = $row['cate_name'];
            }

            return $cate;
        }

        public function cat_one()
        {
            $query  = $this->db->from('category')->select('cate_id,cate_name,pid')->where(['is_delete' => 0, 'is_show' => 0])->get();
            $query  = $query->result_array();
            $result = $this->getTree($query);
            return $result;
        }

        function getTree($array, $pid = 0, $level = 0, &$list = [])
        {

            foreach ($array as $key => $value) {
                //第一次遍历,找到父节点为根节点的节点 也就是pid=0的节点
                if ($value['pid'] == $pid){
                    //父节点为根节点的节点,级别为0，也就是第一级
                    $value['level'] = $level;
                    //把数组放到list中
                    $list[] = $value;
                    //把这个节点从数组中移除,减少后续递归消耗
                    unset($array[$key]);
                    //开始递归,查找父ID为该节点ID的节点,级别则为原级别+1
                    $this->getTree($array, $value['cate_id'], $level + 1, $list);
                }
            }
            return $list;
        }

        public function cate_count($param = array())
        {
            $this->db->from('category');
            $this->db->where("cate_name LIKE '%{$param['cate_name']}%'");
            //$this->_where($param);

            return $this->db->count_all_results();
        }

        public function cate_list($param = array())
        {
            $this->db->from('category')->order_by('cate_id desc')->limit($this->limit_rows, $this->limit_rows * $param['page']);
            if ($param['cate_name']){
                $this->db->where("cate_name LIKE '%{$param['cate_name']}%'");
            }
            $query = $this->db->get();
            $tags  = $query->result_array();
            return $tags;
        }

        /**
         * 获取格式化分类列表
         * author: walker
         * Date: 2019/11/7
         * Time: 10:16
         * Note:
         */
        public function cate_format_list()
        {
            $this->db
                ->from('category')
                ->order_by('cate_id desc')
                ->select("*");
            $query = $this->db->get();
            $tags  = $query->result_array();
//            foreach ($tags as $key => $val) {
//                if(empty($val['goods_count'])){
//                    $count_query = $this->db->select("");
//                    $tags[$key]['goods_count'] =$count_query->result_array();
//                }
//            }
            return $this->getCateTree($tags);
        }

        /**
         * 无限极分类树 getTree($categories)
         *
         * @param array $data
         * @param int   $parent_id
         * @param int   $level
         *
         * @return array
         */
        function getCateTree($data = [], $pid = 0, $level = 0)
        {
            $tree = [];
            if ($data && is_array($data)){
                foreach ($data as $v) {
                    if ($v['pid'] == $pid){
                        $tree[] = array(
                            'cate_id'   => $v['cate_id'],
                            'level'     => $level,
                            'cate_name' => $v['cate_name'],
                            'pid'       => $v['pid'],
                            'children'  => $this->getTree($data, $v['cate_id'], $level + 1),
                        );

                    }
                }
            }
            return $tree;
        }

        //修改状态
        public function cate_up_show($cate_id)
        {
            $query   = $this->db->from('category')->where(['cate_id' => $cate_id])->select('is_show')->get();
            $is_show = $query->row_array();
            $is_show['is_show'] == 0 ? $val = 1 : $val = 0;
            $this->db->where(['cate_id' => $cate_id])->update('category', ['is_show' => $val]);
        }

        public function cate_add($data)
        {
            $this->db->insert('category', $data);
        }

        public function cate_find($cate_id)
        {
            $this->db->from('category');
            $this->db->where('cate_id', $cate_id);
            $query      = $this->db->get();
            $order_info = $query->row_array();
            return $order_info;
        }

        public function cate_update($param, $hidden_id)
        {
            $this->db->where('cate_id', $hidden_id);
            $this->db->update('category', $param);
        }

    }